package cetcbigdata.da.business_environment.beijing;

import cetcbigdata.da.base.Base;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.List;

/**
 * @author YuanGongPing
 * @version 0.1
 * @description SpiderBeiJing
 * @since 2022/9/19 9:36
 */


public class SpiderBeiJing extends Base {
    private static String baseRegion = "北京市";

    private static String baseModule = "营商环境";


    @Override
    public JSONObject getDataDemo() {
        try {
            return processResult("xxx", "xxx", "xxx", "xxx", new JSONObject(), "xxx", "xxx");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {
        List<JSONObject> classificationList  = getClassification();
        List<JSONObject> serviceAreaList  = getServiceAreaById();
        for(JSONObject classification : classificationList){
            for (JSONObject serviceArea : serviceAreaList){
                String classificationStr =  classification.getString("typeKey");
                String classificationName =  classification.getString("name");
                String module = baseModule + "-" + classificationName;
                String region = baseRegion + "-" + serviceArea.getString("locationName");
                parseListPage(serviceArea.getString("id"), classificationStr, module, region, recordSender, fieldsList);
            }
        }
    }

    private static  JSONObject parseListPage(String locationCode, String periodType, String module, String region, RecordSender recordSender, List<Object> fieldsList){
        // 初始化总页码数
        int pages = 0;
        // 当前页
        int currentPage = 1;
        // 如果当前也不等于总页码数，则一直循环 ，实现翻页
        while (currentPage != pages){
            try {
                String listUrl = "http://banshi.beijing.gov.cn/guideservice/pubtask/getTaskList";
                JSONObject parmas = new JSONObject();
                parmas.put("locationCode", locationCode);
                parmas.put("localType", "0");
                parmas.put("businessType", "");
                parmas.put("periodType", periodType);
                parmas.put("taskName", "");
                parmas.put("deptCode", "");
                parmas.put("taskType", "");
                parmas.put("pageNum", currentPage);
                String listResponse =  getReq(listUrl, UtilBeiJing.getHeaders(), parmas);
                JSONObject responseObj = JSONObject.parseObject(listResponse);
                if(responseObj.getString("code").equals("200")){
                    JSONArray list = responseObj.getJSONObject("data").getJSONArray("list");

                    for(Object o : list){
                        JSONObject item = (JSONObject) o;
                        // 如果存在子选项， 先采集父节点信息，然后再采集子节点信息，父节点信息只存在基本信息页面，子节点信息则和其他情况一样
                        if(item.containsKey("childNode")){
                            String detailUrl = "https://banshi.beijing.gov.cn" + item.getString("taskUrl");
                            JSONArray childNodeList = item.getJSONArray("childNode");
                            JSONObject content = new JSONObject();
                            JSONArray contentChildList = new JSONArray();
                            for(Object oChild : childNodeList){
                                JSONObject contentSub = new JSONObject();
                                JSONObject itemChild = (JSONObject) oChild;
                                contentSub.put("名称", itemChild.getString("taskName"));
                                contentSub.put("地址", "https://banshi.beijing.gov.cn" + itemChild.getString("taskUrl"));
                                contentChildList.add(contentSub);
                            }
                            content.put("名称", item.getString("taskName"));
                            content.put("地址", detailUrl);
                            content.put("选项", contentChildList);
                            printDataCount(4);
                            processItem(
                                    processResult(listUrl, listResponse, detailUrl, "", content, region, module),
                                    recordSender,
                                    fieldsList
                            );
                        }
                        else {
                            String detailUrl = "https://banshi.beijing.gov.cn" + item.getString("taskName");
                            JSONObject content = new JSONObject();
                            content.put("名称", detailUrl);
                            content.put("地址", "https://banshi.beijing.gov.cn" + item.getString("taskUrl"));
                            printDataCount(4);
                            processItem(
                                    processResult(listUrl, listResponse, detailUrl, "", content, region, module),
                                    recordSender,
                                    fieldsList
                            );
                        }
                    }
                }
                // 更新当前页
                currentPage = responseObj.getJSONObject("data").getInteger("pageNum") + 1;
                // 更新总页数
                pages = responseObj.getJSONObject("data").getInteger("pages");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return new JSONObject();
    }

    private static List<JSONObject> getClassification(){

        // 获取法人服务按生命周期分类信息，开办企业，申请资质等
        String url = "https://banshi.beijing.gov.cn/guideservice/classification/getClassification?id=3003&locationCode=110000000000";
        String response = getReq(url, UtilBeiJing.getHeaders(), UtilBeiJing.getParams());
        JSONObject data = JSONObject.parseObject(response);
        List<JSONObject> out = new ArrayList<>();
        if(data.getString("code").equals("200")){
            JSONArray dataList = data.getJSONArray("data");
            for(Object o : dataList){
                JSONObject oo = (JSONObject) o;
                out.add(oo);
            }
        }
        return out;
    }


    private static List<JSONObject> getServiceAreaById(){
        // 获取北京市下所有区
        String url = "https://banshi.beijing.gov.cn/guideservice/serviceArea/getServiceAreaById?id=110000000000";
        String response = getReq(url, UtilBeiJing.getHeaders(), UtilBeiJing.getParams());
        JSONObject data = JSONObject.parseObject(response);
        List<JSONObject> out = new ArrayList<>();
        if(data.getString("code").equals("200")){
            JSONArray dataList = data.getJSONObject("data").getJSONArray("childNode");
            for(Object o : dataList){
                JSONObject oo = (JSONObject) o;
                out.add(oo);
            }
        }
        return out;
    }


    public static void main(String[] args) {
        SpiderBeiJing spider = new SpiderBeiJing();
        spider.getData(null, new ArrayList<>());
    }
}
